<?php

class Event_model extends  Model {


    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }


    public function get_event_list($limit,$start) {
         
         try{
        $event_sql = "SELECT *,
                     (select lvalue from tbl_lang b where b.lkey=a.eventtile and b.lang='UK') as eventtitle
                                    FROM tbl_events a ";
            $event_sql.= " order by a.fromdate " ;
            $event_sql.= " limit ".$start.",".$limit;
           
        $event = $this->db->query($event_sql);
        if($event->num_rows() > 0) {
                 return $event_result = $event->result_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }
    
    public function get_event_list_for_featuredevents() {

         try{
            $event_sql = "SELECT *,
                (select lvalue from tbl_lang b where b.lkey=a.eventtile and b.lang='UK') as eventtile
                                    FROM tbl_events a  where status=1";
        $event = $this->db->query($event_sql);
        if($event->num_rows() > 0) {
                 return $event_result = $event->result_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }

        public function get_featuredevent_for_featuredevents() {

         try{
            $event_sql = "SELECT *
                                    FROM tbl_events where status=1 and iseventofthemonth=1 ";
        $event = $this->db->query($event_sql);
        if($event->num_rows() > 0) {
                 return $event_result = $event->row_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }

    
    public function get_event_list_count() {
         try{
            $event_sql = "SELECT count(*) as count
                                    FROM tbl_events";
        $event = $this->db->query($event_sql);
        if($event->num_rows() > 0) {
                  $event_result = $event->row_array();
                  return $event_result["count"];
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }
        public function get_event_list_forhoteledit($hotel_id) {

         try{
            $event_sql = "SELECT *  ,  (select accid from tbl_hotelevents b  where b.Hid={$hotel_id} and b.accid=a.id) as checkval FROM tbl_events a
                                    
                       ";
        $event = $this->db->query($event_sql);
        if($event->num_rows() > 0) {
                 return $event_result = $event->result_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }

     public function addnewevent($eventdetails,$langarr) {
        $status=(!empty($eventdetails["status"]))?$eventdetails["status"]:0;
        try{
                $event_sql = "INSERT INTO tbl_events
                                              (
                                               fromdate,
                                               status
                                              )
                                       VALUES ('{$eventdetails["event_start"]}',{$status})";
               $this->db->query($event_sql);
               $last_insert_id = $this->db->insert_id();
               $event_title_langkey = $last_insert_id . "_eventtitle";
               $event_desc_langkey = $last_insert_id . "_eventdesc";
               $updatesql="update tbl_events  set eventtile='{$event_title_langkey}', eventdsc='{$event_desc_langkey}' where id={$last_insert_id} ";
               $queryupdate = $this->db->query($updatesql);
               if($queryupdate)
               {
                    foreach ($langarr as $langkey=>$langval)
                        {
                             $sql = "INSERT INTO tbl_lang(lkey, lang, lvalue, mod_id, module)
                                    VALUES('" . $event_title_langkey . "', '" . $langkey . "', '" . $eventdetails["event_detail_arr_lang"]["title" . $langkey] . "', " . $last_insert_id . ", 'event')";
                            $query = $this->db->query($sql);
                            if($query)
                            {
                                 $sql = "INSERT INTO tbl_lang(lkey, lang, lvalue, mod_id, module)
                                    VALUES('" . $event_desc_langkey . "', '" . $langkey . "', '" . $eventdetails["event_detail_arr_lang"]["desc" . $langkey] . "', " . $last_insert_id . ", 'event')";
                                $query = $this->db->query($sql);

                            }
                        }
                     
                }
                $this->createjsonfile($langarr);
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }

    public function saveevents() {
        try{
               /* $event_sql = "INSERT INTO tbl_events
                                              (
                                               accdesc,
                                               status
                                              )
                                       VALUES (?,?)";
               $this->db->query($event_sql, array(
                                             $acomdationdetails['txttitle'],
                                             $acomdationdetails['txtStatus'],

                ));*/
            $this->createjsonfile();

        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }

    public function createjsonfile($lang=array())
    {
        foreach($lang as $langkey=>$langval)
        {
             $events_sql = "select * ,
                (select lvalue from tbl_lang b where b.lkey=a.eventtile and b.lang='{$langkey}') as eventtitle,
                (select lvalue from tbl_lang b where b.lkey=a.eventdsc and b.lang='{$langkey}') as eventdesc
                     from tbl_events a where a.status=1";
           $events = $this->db->query($events_sql, array());
           $events_result = $events->result_array();
           // print_r($events_result);
            $jsonfilestring= '[';
            $separator = "";

            foreach($events_result as $val) {
                    $jsonfilestring.= $separator;
                    $initTime = (intval(strtotime($val["fromdate"]))*1000);
                    $jsonfilestring.= '	{ "date": "' . $initTime . '", "type": "test", "title": "'.$val["eventtitle"].'", "description": "'.substr($val["eventdesc"],0,50).'....", "url": "'.  base_url().'events/index/'.$val["id"].'" }';
                    $separator = ",";
            }
            $jsonfilestring.= ' ]';
            $myFile = "json/events_".$langkey.".json";
            $fh = fopen($myFile, 'w') or die("can't open file");
            $stringData =$jsonfilestring;
            fwrite($fh, $stringData);

            fclose($fh); 
        }
           

    }

    public function get_event_details($eventid)
    {
         try{
           $event_sql = "SELECT *,
                     (select lvalue from tbl_lang b where b.lkey=a.eventtile and b.lang='UK') as eventtitleuk,
                     (select lvalue from tbl_lang b where b.lkey=a.eventdsc and b.lang='UK') as eventdscuk,
                     (select lvalue from tbl_lang b where b.lkey=a.eventtile and b.lang='DE') as eventtitlede,
                     (select lvalue from tbl_lang b where b.lkey=a.eventdsc and b.lang='DE') as eventdscde
                                    FROM tbl_events a where a.id={$eventid}  ";
        $event = $this->db->query($event_sql);
        if($event->num_rows() > 0) {
                 return $event_result = $event->row_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }
    
     public function doeditevent($eventdetails,$partnerlang) {
      //   print_r($partnerlang);exit;
          $status=(!empty($eventdetails["status"]))?$eventdetails["status"]:0;
        try{
               $event_sql = "update tbl_events set fromdate='{$eventdetails["event_start"]}',status={$status}  where id={$eventdetails["id"]}";
                 $query=$this->db->query($event_sql);
                  if($query)
                   {
                        foreach($partnerlang as $langkey=>$langval)
                        {
                             $sql_title = "UPDATE tbl_lang SET lvalue = '" . $eventdetails['title' . $langkey] . "'
                                    WHERE lang='" . $langkey . "'
                                        AND mod_id = " . $eventdetails['id'] . "
                                            AND lkey = '" . $eventdetails['id'].'_eventtitle' . "'
                                                AND module = 'event' ";
                            $query_title = $this->db->query($sql_title);

                             $sql_desc = "UPDATE tbl_lang SET lvalue = '" . $eventdetails['desc' . $langkey] . "'
                                    WHERE lang='" . $langkey . "'
                                        AND mod_id = " . $eventdetails['id'] . "
                                            AND lkey='".$eventdetails['id'].'_eventdesc'."'
                                                AND module = 'event';";
                            $query_desc = $this->db->query($sql_desc);
                        }
                   }
          $this->createjsonfile($partnerlang);
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }

      public function deleteevent($accid,$partnerlang) {
      //   print_r($acomdationdetails);exit;
        try{
                $event_sql = "delete from tbl_events  where id=?";
               $this->db->query($event_sql, array($accid
                ));
       $this->createjsonfile($partnerlang);
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }

    /*public function get_featuredevent_list() {
        /*$event_sql = "SELECT *
                                    FROM tbl_featuredaccommodation";
        $event = $this->db->query($event_sql); * /
         try{
            $event_sql = "SELECT *
                                    FROM tbl_featuredaccommodation";
        $event = $this->db->query($event_sql);
        if($event->num_rows() > 0) {
                 return $event_result = $event->result_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }*/
    
    public function updatefeaturedevent($eventid) {
        try{            
            $sqlupdate=" update tbl_events set iseventofthemonth=0 ";
            $updatall=$this->db->query($sqlupdate);
            if($updatall)
            {
                $sqlupdatefeatured=" update tbl_events set iseventofthemonth=1 where id={$eventid} ";
                 $update=$this->db->query($sqlupdatefeatured);
                 return $update;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }

     public function updatefeaturedeventimage($eventid,$image) {
        try{
            $sqlupdate=" update tbl_events set image='{$image}' where id={$eventid} ";
            $updatall=$this->db->query($sqlupdate);
             
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }
    
    public function is_featured_event_available()
    {        
         try{
           $event_sql = "SELECT id
                                    FROM tbl_featuredaccommodation order by `id` desc limit 1";
        $event = $this->db->query($event_sql);
        if($event->num_rows() > 0) {
                 return $event_result = $event->row_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }
    
    public function get_featured_event_details($featuredaccid)
    {
        
         try{
           $event_sql = "SELECT *
                                    FROM tbl_featuredaccommodation where `id`=?";
        $event = $this->db->query($event_sql, array($featuredaccid));
        if($event->num_rows() > 0) {
                 return $event_result = $event->row_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }
    
     public function doeditfeaturedacomdation($acomdationdetails) {
      //   print_r($acomdationdetails);exit;
        try{
                $event_sql = "update tbl_featuredaccommodation set `title`=?,`desc`=?,`date_from`=?,`date_to`=?,`isactive`=?  where `id`=?";
               $this->db->query($event_sql, array(
                                                        $acomdationdetails['title'],
                                                        $acomdationdetails['desc'],
                                                        $acomdationdetails['date_from'],
                                                        $acomdationdetails['date_to'],
                                                        $acomdationdetails['isactive'],
                                                        $acomdationdetails['id']
                ));

        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }   
    
    /*public function deletefeaturedacomdation($faccid) {
      //   print_r($acomdationdetails);exit;
        try{
            $event_sql = "delete from tbl_featuredaccommodation  where `id`=?";
            $this->db->query($event_sql, array($faccid
            ));

        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }*/
    

}
?>
